<template>
  <div>
    <el-row class="bottom-space">
      <el-col :span="6">
        <div>
          <div style="display: inline-block">
            <el-button :style="{ display: editButtonEnable }" type="primary" @click="openNewKnowledgeLib">新建知识库
            </el-button>
          </div>
          <div style="display: inline-block">
            <el-button :style="{ display: editButtonEnable }" plain type="primary" @click="openExportResult">知识导出申请结果
            </el-button>
          </div>
        </div>
      </el-col>
      <el-col :offset="8" :span="10">
        <div>
          <el-input v-model="searchValue" placeholder="请输入知识库名称" @keyup.enter.native="refreshTable">
            <el-select @change="refreshTable" collapse-tags multiple placeholder="请选择绑定平台" slot="prepend"
                       v-model="selectedPlatforms"
                       style="width: 180px;">
              <el-option v-for="platform in platforms" :key="platform.id" :label="platform.name" :value="platform.id"/>
            </el-select>
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div>
          <el-table highlight-current-row ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    :height="tableHeight"
                    border
                    tooltip-effect="dark">
            <el-table-column label="知识库名称" prop="name" width="240"/>
            <el-table-column label="全部字段" prop="fieldList">
              <template slot-scope="scope">
                <span >{{ scope.row.fieldList && scope.row.fieldList.map(field => field.name).join() }}</span>
              </template>
            </el-table-column>
            <el-table-column label="绑定平台" width="200">
              <template slot-scope="scope">
                <span style="color: #2DB7A0">{{ scope.row.platformList && scope.row.platformList.map(field => field.name).join() }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="330">
              <template slot-scope="scope">
                <el-button :style="{ display: detailButtonEnable }" size="small" type="text"
                           @click="openKnowledgeDetail(scope.row.id)">详情
                </el-button>
                <el-button v-if="scope.row.editFlag" :style="{ display: editButtonEnable }" size="small" type="text"
                           @click="openReleased(scope.row.id)">
                  <span :style="{color : scope.row.waitToPublishTaskItemCount>0 ? 'red' : 'black'}">发布</span>
                </el-button>
                <el-button v-if="scope.row.pushFlag" :style="{ display: pushButtonEnable }" size="small" type="text"
                           @click="openPush(scope.row.id, scope.row.isToPlatform)">推送
                </el-button>
                <!--                <el-button :style="{ display: editButtonEnable }" size="small" type="text"-->
                <!--                           @click="exportWord(scope.row.id, scope.row.name)">导出word-->
                <!--                </el-button>-->
                <el-button v-if="scope.row.editFlag" :style="{ display: editButtonEnable }" size="small" type="text"
                           @click="openSetting(scope.row.id, scope.row.name)">设置
                </el-button>
                <el-button v-if="scope.row.editFlag" :style="{ display: editButtonEnable }" size="small" type="text"
                           @click="handleDelete(scope.row.id)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-pagination
        :page-size.sync="pageSize"
        :current-page.sync="currentPage"
        :page-sizes="[5, 10, 15, 20, 50, 100]"
        :total="totalNum"
        @size-change="refreshTable"
        @current-change="refreshTable"
        background
        layout="total, sizes, prev, pager, next, jumper"
        style="float: right">
      </el-pagination>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "KnowledgeLibManage",
  data() {
    return {
      tableData: [],
      totalNum: 0,
      pageSize: 10,
      currentPage: 1,
      searchValue: "",
      tableHeight: 0,
      content1: '',
      selectedPlatforms: [],
      platforms: [{
        id: "",
        name: ""
      }],
      editButtonEnable: '',
      pushButtonEnable: '',
      detailButtonEnable: ''
    }
  },
  methods: {
    openNewKnowledgeLib() {
      this.$router.push({
        path: "/new",
        query: {
          specialtyId: this.$store.state.specialtyId
        }
      })
    },
    openExportResult() {
      this.$router.push("/exportApplyResultList");
    },
    openReleased(id) {
      this.$router.push({
        path: "/temp",
        query: {
          id: id,

        }
      });
    },
    openPush(id, isToPlatform) {
      this.$router.push({
        path: "/syn",
        query: {
          id: id,
          isToPlatform: isToPlatform
        }
      });
    },
    openKnowledgeDetail(id) {
      this.$router.push({
        path: "/temp",
        query: {
          id: id,
          showStatus: 'detailType',
          type: 'detail'
        }
      });
    },
    openSetting(id, name) {
      this.$router.push({
        path: "/new",
        query: {
          id: id,
          name: name
        }
      });
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该知识库, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.axios.get("/pmph/action/knowledgeLib/delete", {
          params: {
            id: id
          }
        }).then(() => {
            this.refreshTable()
        }).catch((err) => {
          console.error(err);
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    refreshTable() {
      this.axios.get("/pmph/action/knowledgeLib/selectKnowledgeLibPage", {
        params: {
          name: this.searchValue,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          platformIdArray: this.selectedPlatforms,
          specialtyId: this.$store.state.specialtyId
        }
      }).then((res) => {
        this.tableData = res.data.records
        this.totalNum = res.data.total
      })
    },
    getAllPlatforms() {
      this.axios.get("/pmph/action/knowledgeLib/selectAllPlatforms", {
        params: {
          type: CONST.SYSTEM.KNOWLEDGE_LIB
        }
      }).then((res) => {
        this.platforms = res.data.platforms
      }).catch((err) => {
        console.error(err)
      })
    },
    exportWord(knowledgeLibId, knowledgeLibName) {
      this.axios.get("/pmph/action/knowledgeLib/exportWord", {
        params: {
          knowledgeLibId: knowledgeLibId
        },
        responseType: 'blob',
        headers: {
          'Content-Type': 'application/octet-stream'
        }
      }).then((res) => {
        let data = res.data
        let url = window.URL.createObjectURL(new Blob([data], {type: "application/octet-stream"}));
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', knowledgeLibName + '模板.docx');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link); //下载完成移除元素
        window.URL.revokeObjectURL(url); //释放blob对
      })
    },
  },
  created() {
    // 存储在store里面,目的是点击详情按钮后,再点击新建,仍保证该知识库术语某一专科库
    this.$store.commit('setSpecialtyId', this.$route.query.specialtyId)
    if (this.$store.state.user.userName === 'admin') {
      return
    }
    //角色Id等于管理员角色
    for (let i = 0; i < this.$store.state.user.roleIds.length; i++) {
      if (this.$store.state.user.roleIds[i] === '1455781731549327361') {
        return;
      }
    }
    UTIL.findTreeNode(this.$store.state.menuList, item => {
      if (item.id === this.$store.state.activeId) {
        if (item.editButton) {
          this.editButtonEnable = ''
        } else {
          this.editButtonEnable = 'none'
        }
        if (item.pushButton) {
          this.pushButtonEnable = ''
        } else {
          this.pushButtonEnable = 'none'

        }
        if (item.detailButton) {
          this.detailButtonEnable = ''
        } else {
          this.detailButtonEnable = 'none'
        }
      }

    })

    // 调整表格所在行的高度
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 106
  },
  activated() {
    if (this.$route.meta.isUseCache) {
      return
    }
    this.refreshTable()
    this.getAllPlatforms()
    // 调整表格所在行的高度
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 95
  }
}
</script>

<style scoped>

.bottom-space {
  margin-bottom: 10px;
}
</style>
